package com.huashizhaoxi.lionapp.controller;

import com.huashizhaoxi.lionapp.entity.FruitItem;
import com.huashizhaoxi.lionapp.entity.FruitKind;
import com.huashizhaoxi.lionapp.entity.Result;
import com.huashizhaoxi.lionapp.entity.User;
import com.huashizhaoxi.lionapp.service.FruitService;
import com.huashizhaoxi.lionapp.service.UserService;
import com.huashizhaoxi.lionapp.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.*;

@RestController
@RequestMapping("/fruit")
public class FruitController {

    @Autowired
    private FruitService fruitService;

    @Autowired
    private UserService userService;

    private JwtUtils jwtUtils;

    @GetMapping("/getFruitKind")
    public Result getFruitKind() {
        List<FruitKind> fruitKindList = fruitService.getFruitKind();
        if(fruitKindList == null) {
            return Result.error("获取果切种类失败");
        }else{
            return Result.success("获取果切种类成功",fruitKindList);
        }
    }

    @PostMapping("/addFruitKind")
    public Result addFruitKind(@RequestBody FruitKind fruitKind, @RequestHeader("mytoken") String token) {
        Map<String,Object> map = jwtUtils.parseJwt(token);
        if(map == null) {
            return Result.error("身份验证失败：未登录");
        }
        User user = userService.getUserByOpenId((String) map.get("open_id"));
        if(user.getRole() != 3 && user.getRole() != 2) {
            return Result.error("身份验证失败：无权限");
        }
        fruitKind.setStatus(0);
        int res = fruitService.addFruitKind(fruitKind);
        if(res == 0) {
            return Result.error("添加果切种类失败");
        }else{
            return Result.success("添加果切种类成功",null);
        }
    }

    @PostMapping("/addFruitItem")
    public Result addFruitItem(@RequestBody FruitItem fruitItem, @RequestHeader("mytoken") String token) {
        Map<String,Object> map = jwtUtils.parseJwt(token);
        if(map == null) {
            return Result.error("身份验证失败：未登录");
        }
        User user = userService.getUserByOpenId((String) map.get("open_id"));
        if(user.getRole() != 3 && user.getRole() != 2) {
            return Result.error("身份验证失败：无权限");
        }
        fruitItem.setSell_count(0);
        int res = fruitService.addFruitItem(fruitItem);
        if(res == 0) {
            return Result.error("添加果切商品失败");
        }else{
            return Result.success("添加果切商品成功",null);
        }
    }

    @DeleteMapping("/deleteFruitItem/{id}")
    public Result deleteFruitItem(@PathVariable("id") int id, @RequestHeader("mytoken") String token) {
        Map<String,Object> map = jwtUtils.parseJwt(token);
        if(map == null) {
            return Result.error("身份验证失败：未登录");
        }
        User user = userService.getUserByOpenId((String) map.get("open_id"));
        if(user.getRole() != 3 && user.getRole() != 2) {
            return Result.error("身份验证失败：无权限");
        }
        int res = fruitService.deleteFruitItem(id);
        if(res == 0) {
            return Result.error("删除果切商品失败");
        }else{
            return Result.success("删除果切商品成功",null);
        }
    }

    @DeleteMapping("/deleteFruitKind/{id}")
    @Transactional(rollbackFor = Exception.class)
    public Result deleteFruitKind(@PathVariable("id") int id, @RequestHeader("mytoken") String token) {
        try{
            Map<String,Object> map = jwtUtils.parseJwt(token);
            if(map == null) {
                return Result.error("身份验证失败：未登录");
            }
            User user = userService.getUserByOpenId((String) map.get("open_id"));
            if(user.getRole() != 3 && user.getRole() != 2) {
                return Result.error("身份验证失败：无权限");
            }
            fruitService.deleteFruitItemByKindId(id);
            fruitService.deleteFruitKind(id);
            return Result.success("删除果切种类成功",null);
        }catch (Exception e) {
            return Result.error("删除果切种类失败");
        }
    }

    @PutMapping("/reviseFruitKind")
    @Transactional(rollbackFor = Exception.class)
    public Result reviseFruitKind(@RequestBody List<FruitKind> fruitKindList,@RequestHeader("mytoken") String token){
        try{
            Map<String,Object> map = jwtUtils.parseJwt(token);
            if(map == null) {
                return Result.error("身份验证失败：未登录");
            }
            User user = userService.getUserByOpenId((String) map.get("open_id"));
            if(user.getRole() != 3 && user.getRole() != 2) {
                return Result.error("身份验证失败：无权限");
            }
            for(FruitKind fruitKind:fruitKindList) {
                fruitService.reviseFruitKind(fruitKind);
            }
            return Result.success("修改果切种类成功",null);
        } catch(Exception e) {
            return Result.error("修改果切种类失败");
        }
    }

    @PutMapping("/reviseFruitItem")
    @Transactional(rollbackFor = Exception.class)
    public Result reviseFruitItem(@RequestBody List<FruitItem> fruitItemList,@RequestHeader("mytoken") String token){
        try{
            Map<String,Object> map = jwtUtils.parseJwt(token);
            if(map == null) {
                return Result.error("身份验证失败：未登录");
            }
            User user = userService.getUserByOpenId((String) map.get("open_id"));
            if(user.getRole() != 3 && user.getRole() != 2) {
                return Result.error("身份验证失败：无权限");
            }
            for(FruitItem fruitItem:fruitItemList) {
                fruitService.reviseFruitItem(fruitItem);
            }
            return Result.success("修改果切商品成功",null);
        } catch(Exception e) {
            return Result.error("修改果切商品失败");
        }
    }



    @GetMapping("/getValidFruitKind")
    public Result getValidFruitKind(){
        List<FruitKind> fruitKindList = fruitService.getValidFruitKind();
        if(fruitKindList == null) {
            return Result.error("获取有效果切种类失败");
        }else{
            return Result.success("获取有效果 切种类成功",fruitKindList);
        }
    }

    @GetMapping("/getFruitItem")
    public Result getFruitItem() {
        List<FruitItem> fruitItemList = fruitService.getFruitItem();
        if(fruitItemList == null) {
            return Result.error("获取果切商品失败");
        }else{
            return Result.success("获取果切商品成功",fruitItemList);
        }
    }

    @GetMapping("/getFruitItemByKind/{id}")
    public Result getFruitItemByKind(@PathVariable(value = "id") int fruit_buy_kind_id) {
        List<FruitItem> fruitItemList = fruitService.getFruitItemByKind(fruit_buy_kind_id);
        if(fruitItemList == null) {
            return Result.error("获取果切商品失败");
        }else{
            return Result.success("获取果切商品成功",fruitItemList);
        }
    }

    @GetMapping("/getFruitPage")
    public Result getFruitPage(){
        List<Map<String,Object>> mapList = new ArrayList<>();
        List<FruitKind> fruitKindList = fruitService.getValidFruitKind();
        List<FruitItem> returnFruitItemList = new ArrayList<>();
        for(int i = 0; i < fruitKindList.size(); i++) {
            List<FruitItem> fruitItemList = fruitService.getFruitItemByKind(fruitKindList.get(i).getId());
            // 在returnFruitItemList最后拼上fruitItemList
            returnFruitItemList.addAll(fruitItemList);
            Map<String, Object> map1 = new HashMap<>();
            map1.put("fruitKindId", fruitKindList.get(i).getId());
            map1.put("fruitKindName", fruitKindList.get(i).getName());
            map1.put("fruitItemList", fruitItemList);
            mapList.add(map1);
        }
        Map<String, Object> map2 = new HashMap<>();
        map2.put("fruitKindList", fruitKindList);
        map2.put("fruitItemList", mapList);
        map2.put("returnFruitItemList", returnFruitItemList);
        return Result.success("获取果切商品成功",map2);
    }

}
